package com.simon.hse.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.google.gson.Gson;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.fragment.HomeFragment;
import com.simon.hse.fragment.MessageFragment;
import com.simon.hse.fragment.MineFragment;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.DropDownInput;
import com.simon.hse.http.entity.DropDownOutput;
import com.simon.hse.utils.PreferencesUtils;
import com.simon.hse.utils.ToastUtils;
import com.simon.hse.utils.XPermission;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;

@SuppressWarnings("all")
@ContentView(R.layout.activity_main)
public class HomeActivity extends BaseActivity {
    @ViewInject(R.id.tablayout)
    TabLayout mTablayout;

    @ViewInject(R.id.vp_container)
    ViewPager mVp_container;

    private String[] tabs = new String[]{"首页", "消息", "我的"};
    private int[] imgs = new int[]{R.drawable.selector_tab1, R.drawable.selector_tab2, R.drawable.selector_tab3};
    private boolean isExit = false;

    // AddTrainRecordActivity -- 上传图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean login_state = PreferencesUtils.getBoolean("LOGIN_STATE", false);
        if (login_state) {
            initDatas();
            initFactory();
//            HttpManager.getInstance().updateCookie(new CommonCallBack<String>() {
//                @Override
//                public void receiveMsg(String msg) {
//                    if (msg.equals("Success")) {
//                        initDatas();
//                    }
//                }
//            });
        } else {
            startActivity(new Intent(HomeActivity.this, LoginActivity.class));
            finish();
        }

    }

    private void initDatas() {
        mVp_container.setOffscreenPageLimit(3);
        mVp_container.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                switch (i) {
                    case 0:
                        return new HomeFragment();
                    case 1:
                        return new MessageFragment();
                    case 2:
                        return new MineFragment();
                    default:
                        return new HomeFragment();
                }
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }

            @Override
            public int getCount() {
                return tabs.length;
            }
        });
        mTablayout.setupWithViewPager(mVp_container);

        setDownTabs();
    }

    /**
     * 工厂
     */
    private void initFactory() {
        DropDownInput dropDownInput = new DropDownInput();
        List<String> categoryList = new ArrayList<>();
        categoryList.add("9001");
        List<String> functionList = new ArrayList<>();
        dropDownInput.setCategoryCodeArray(categoryList);
        dropDownInput.setFunctionCodeArray(functionList);
        dropDownInput.setIsGetFactoryList(false);

        Call<String> call = HttpManager.getInstance().requestService().DROP_DOWN(HttpManager.getInstance().requestBody(dropDownInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                DropDownOutput dropDownOutput = new Gson().fromJson(result, DropDownOutput.class);
                if (dropDownOutput.getResult().equals("Success")) {
                    List<DropDownOutput.DataBean.DataDicListBean> dataDicList = dropDownOutput.getData().getDataDicList();
                    for (DropDownOutput.DataBean.DataDicListBean dataDicListBean : dataDicList) {
                        if (dataDicListBean.getKey().equals("9001")) {
                            List<DropDownOutput.DataBean.DataDicListBean.ValueBean> valueBeanList = dataDicListBean.getValue();
                            MyApplication.getInstance().mFactoryList.clear();
                            MyApplication.getInstance().mFactoryList.addAll(valueBeanList);
                        }
                    }
                } else {
                    ToastUtils.getInstance().showShortToastBottom(dropDownOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 设置tab
     */
    private void setDownTabs() {
        mTablayout.getTabAt(0).setCustomView(getTabView(0));
        mTablayout.getTabAt(1).setCustomView(getTabView(1));
        mTablayout.getTabAt(2).setCustomView(getTabView(2));
    }

    /**
     * 自定义tab view
     *
     * @param position
     * @return
     */
    public View getTabView(int position) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_custom_tab, null);
        ImageView iv_tab = (ImageView) inflate.findViewById(R.id.iv_tab);
        TextView tv_tab = (TextView) inflate.findViewById(R.id.tv_tab);
        iv_tab.setImageResource(imgs[position]);
        tv_tab.setText(tabs[position]);
        return inflate;
    }

    /*双击退出*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();// 这里也可以弹出对话框
        }
        return false;
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            ToastUtils.getInstance().showShortToastBottom("再按一次退出程序");
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            // 清空activity
            // ActivityCollector.removeAllActivity();
            XActivityStack.getInstance().finishAllActivity();
        }
    }

}
